package com.chenqi.controller;

import com.chenqi.model.entity.Relationship;
import com.chenqi.service.RelationshipService;
import com.chenqi.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 用户关系控制层
 * @program: ardaicommunity
 * @author: ChenQi
 * @create: 2021-03-2021/3/13-09-11
 */
@RestController
@RequestMapping("/api/v1/relationship")
public class RelationshipController {

    @Autowired
    private RelationshipService relationshipService;

    /**
     * 关注用户
     * @param pid
     * @param fid
     * @return
     */
    @PostMapping("/subscribe/{parentId}/{followerId}")
    public JsonData subscribe(@PathVariable("parentId") Integer pid,
                              @PathVariable("followerId") Integer fid) {
        int i = relationshipService.subscribe(pid, fid);
        return i == 0 ? JsonData.BuildError("关注失败") : JsonData.BuildSuccess("关注成功");
    }

    /**
     * 取关用户
     * @param pid
     * @param fid
     * @return
     */
    @DeleteMapping("/unsubscribe/{parentId}/{followerId}")
    public JsonData unSubscribe(@PathVariable("parentId") Integer pid,
                                @PathVariable("followerId") Integer fid){
        int i = relationshipService.unsubscribe(pid, fid);
        return i == 0 ? JsonData.BuildError("取消关注失败") : JsonData.BuildSuccess("取消关注成功");
    }

    /**
     * 是否关注
     * @param pid
     * @param fid
     * @return
     */
    @GetMapping("/validate/{parentId}/{followerId}")
    public JsonData validate(@PathVariable("parentId") Integer pid,
                             @PathVariable("followerId") Integer fid) {
        Relationship relationship = relationshipService.validate(pid, fid);
        return relationship == null ? JsonData.BuildError("暂未关注") : JsonData.BuildSuccess("已关注");
    }
}
